<?php
	class jingquControl extends commonControl{
		/**
		 * 显示所有景区
		 */
		public function index(){
			$this->get_jq_info();
			$this->display();
		}
		/**
		 * 增加景区视图
		 */
		public function add_show(){
			$cid = isset($_GET['cid']) && !empty($_GET['cid'])?$_GET['cid']:false;
			if($cid === false){
				xiaoquControl::get_citys();
			}
			$db = M('citys');
			$city = $db->find($cid);
			$this->assign('city',$city);
			$this->display();
		}
		/**
		 * 执行增加景区
		 */
		public function add_exe(){
			$db = M("jingqu");
			$db->validate = array(
					array('jname','nonull','景区名称不能为空',2),
				);
			if(!$db->validate()){
				$this->error($db->error);
			}else{
				if($db->add()){
					$this->success("添加成功",'index');
				}else{
					$this->error("添加失败");
				}
			}
		}
		/**
		 * 景区修改
		 */
		public function modify_show(){
			$jid = isset($_GET['jid']) && !empty($_GET['jid'])?$_GET['jid']:false;
			if($jid === false){
				$this->error("参数错误");
			}
			$this->get_jq_info($jid);
			$this->display();
		}
		/**
		 * 执行景区修改
		 */
		public function modify_exe(){
			P($_POST);
			$db = M("jingqu");
			if($db->update()){
				$this->success("修改成功",'index');
			}else{
				$this->error("修改失败");
			}
		}
		/**
		 * 批量修改景区名称
		 */
		public function piliang(){
			$db = M("jingqu");
			foreach($_POST as $v){
				$db->update($v);
			}
			$this->success("修改成功",'index');
		}
		/**
		 * 改变景区状态
		 */
		public function status(){
			$jid = isset($_GET['jid']) && !empty($_GET['jid'])?$_GET['jid']:false;
			if($jid === false){
				$this->error("参数错误");
			}
			$set = $_GET['set'];
			$db = M("jingqu");
			if($db->where(" jid = '".$jid."'")->update(array('status'=>$set))){
				$this->success('修改成功','index');
			}else{
				$this->error("修改失败");
			}
		}
		/**
		 * 获取景区信息
		 */
		public function get_jq_info($jid=null){
			$db = V("jingqu");
			$db->view = array(
					'citys'=>array(
							'type'=>'inner',
							'field'=>'cid,cname',
							'on'=>'jingqu.cid = citys.cid',
					),
			);
			if(is_null($jid)){
				$jingqus = $db->order("jingqu.cid desc")->all();
				$this->assign('jingqus', $jingqus);
			}else{
				$jingqu = $db->find($jid);
				$this->assign("jingqu",$jingqu);
			}
		}
		/**
		 * 执行景区删除
		 */
		public function del(){
			$jid = isset($_GET['jid']) && !empty($_GET['jid'])?$_GET['jid']:false;
			if($jid === false){
				$this->error("参数错误");
			}
			$db = M("house");
			if($db->where(" jid = '".$jid."'")->find()){
				$this->error("该景区下还有房屋，不能删除");
			}else{
				if($db->table('jingqu')->where(" jid = '".$jid."'")->del()){
					$this->success("删除成功");
				}else{
					$this->error('删除失败');
				}
			}
		}
	}

?>